package com.gymmsystem.GYMMSystem.controller;

import com.gymmsystem.GYMMSystem.common.Result;
import com.gymmsystem.GYMMSystem.entity.Coaches;
import com.gymmsystem.GYMMSystem.service.CoachesService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/coaches")
public class CoachesController {

    @Autowired
    private CoachesService coachesService;
    private static final Logger log = LoggerFactory.getLogger(CoachesController.class);

    /**
     * 获取所有教练
     */
    @GetMapping("/all")
    public ResponseEntity<Result<List<Coaches>>> getAllCoaches() {
        try {
            List<Coaches> coaches = coachesService.findAllCoaches();
            return ResponseEntity.ok(Result.success(coaches));
        } catch (Exception e) {
            log.error("获取教练列表失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("获取教练列表失败"));
        }
    }

    /**
     * 根据ID获取教练详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Result<Coaches>> getCoachById(@PathVariable Long id) {
        try {
            Coaches coach = coachesService.getCoachById(id);
            if (coach != null) {
                return ResponseEntity.ok(Result.success(coach));
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND)
                        .body(Result.error("教练不存在"));
            }
        } catch (Exception e) {
            log.error("获取教练详情失败，ID: {}", id, e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("获取教练详情失败"));
        }
    }

    /**
     * 创建新教练
     */
    @PostMapping("/insert")
    public ResponseEntity<Result<Long>> createCoach(@RequestBody Coaches coach) {
        try {
            Long newId = coachesService.createCoach(coach);
            if (newId != null) {
                return ResponseEntity.ok(Result.success(newId, "教练创建成功"));
            } else {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                        .body(Result.error("教练创建失败"));
            }
        } catch (Exception e) {
            log.error("创建教练失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("创建教练失败"));
        }
    }

    /**
     * 更新教练信息
     */
    @PutMapping("/update")
    public ResponseEntity<Result<Boolean>> updateCoach(@RequestBody Coaches coach) {
        try {
            boolean success = coachesService.updateCoachById(coach);
            if (success) {
                return ResponseEntity.ok(Result.success(true, "教练信息更新成功"));
            } else {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                        .body(Result.error("教练信息更新失败"));
            }
        } catch (Exception e) {
            log.error("更新教练信息失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("更新教练信息失败"));
        }
    }

    /**
     * 删除教练
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Result<Boolean>> deleteCoach(@PathVariable Long id) {
        try {
            boolean success = coachesService.deleteCoachById(id);
            if (success) {
                return ResponseEntity.ok(Result.success(true, "教练删除成功"));
            } else {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                        .body(Result.error("教练删除失败"));
            }
        } catch (Exception e) {
            log.error("删除教练失败，ID: {}", id, e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("删除教练失败"));
        }
    }

    /**
     * 根据专长查询教练
     */
    @GetMapping("/specialty/{specialty}")
    public ResponseEntity<Result<List<Coaches>>> getCoachesBySpecialty(@PathVariable String specialty) {
        try {
            List<Coaches> coaches = coachesService.findCoachesBySpecialty(specialty);
            return ResponseEntity.ok(Result.success(coaches));
        } catch (Exception e) {
            log.error("根据专长查询教练失败，专长: {}", specialty, e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("根据专长查询教练失败"));
        }
    }

    /**
     * 根据经验范围查询教练
     */
    @GetMapping("/experience")
    public ResponseEntity<Result<List<Coaches>>> getCoachesByExperienceRange(
            @RequestParam Integer minExperience,
            @RequestParam Integer maxExperience) {
        try {
            List<Coaches> coaches = coachesService.findCoachesByExperienceRange(minExperience, maxExperience);
            return ResponseEntity.ok(Result.success(coaches));
        } catch (Exception e) {
            log.error("根据经验范围查询教练失败，最小经验: {}，最大经验: {}", minExperience, maxExperience, e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("根据经验范围查询教练失败"));
        }
    }

    /**
     * 根据手机号查询教练
     */
    @GetMapping("/phone/{phone}")
    public ResponseEntity<Result<Coaches>> getCoachByPhone(@PathVariable String phone) {
        try {
            Coaches coach = coachesService.findCoachByPhone(phone);
            if (coach != null) {
                return ResponseEntity.ok(Result.success(coach));
            } else {
                return ResponseEntity.status(HttpStatus.NOT_FOUND)
                        .body(Result.error("未找到该手机号对应的教练"));
            }
        } catch (Exception e) {
            log.error("根据手机号查询教练失败，手机号: {}", phone, e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("根据手机号查询教练失败"));
        }
    }

    /**
     * 查询经验丰富的教练（3年以上）
     */
    @GetMapping("/experienced")
    public ResponseEntity<Result<List<Coaches>>> getExperiencedCoaches() {
        try {
            List<Coaches> coaches = coachesService.findExperiencedCoaches();
            return ResponseEntity.ok(Result.success(coaches));
        } catch (Exception e) {
            log.error("查询经验丰富教练失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("查询经验丰富教练失败"));
        }
    }

    /**
     * 根据姓名模糊查询教练
     */
    @GetMapping("/name/{name}")
    public ResponseEntity<Result<List<Coaches>>> getCoachesByName(@PathVariable String name) {
        try {
            List<Coaches> coaches = coachesService.findCoachesByName(name);
            return ResponseEntity.ok(Result.success(coaches));
        } catch (Exception e) {
            log.error("根据姓名查询教练失败，姓名: {}", name, e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Result.error("根据姓名查询教练失败"));
        }
    }
}